﻿using System;
using DT.Application.Http3thApi.WX;
using DT.Application.Http3thApi.WX.Models;
using Furion;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;

namespace DT.Application.Api
{
    /// <summary>
    /// 微信接口 demo
    /// </summary>
    [ApiDescriptionSettings("微信接口")]
    public class WxLoginApiService: IDynamicApiController
    {
        private readonly WXApi _wxapi;

        /// <summary>
        /// 微信服务
        /// </summary>
        /// <param name="wxapi"></param>
        public WxLoginApiService(WXApi wxapi)
        {
            _wxapi = wxapi;
        }

        /// <summary>
        /// 获取微信OpenID
        /// </summary>
        /// <param name="code"></param>
        /// <returns>Code2SessionResult</returns>
        public Code2SessionResult GetWxOpenIdFromCode(string code)
        {
            Code2SessionResult rs = new Code2SessionResult();

            string appid = App.Configuration["weixin:appid"];
            string secret = App.Configuration["weixin:secret"];
            var task =_wxapi.GetWxOpenId(appid, secret, code, "authorization_code");

            rs = task.GetAwaiter().GetResult();

            return rs;
        }
    }
}
